package com.hissagetest;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.* ;
import android.text.style.ForegroundColorSpan;
import android.util.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.hissagetest.HelloSms;
import com.hissagetest.HelloUiTest; 


public class HelloAndroidActivity extends Activity {
	
	private final static int color[] = {Color.BLUE, Color.RED, Color.GREEN} ;
	private final static String logTag = "HelloAndroidActivity";
	private TextView textView ;
	private Button smsButton ;
	private Button uiTestButton ;
	private HelloAndroidActivity self;
	
	private void _dummyLog(String logStr) {
		String textStr = logStr + "\n" ;
        SpannableStringBuilder style=new SpannableStringBuilder(textStr);      
        style.setSpan(new ForegroundColorSpan(color[(int)(Math.random() * 10000)  % 3]), 0, textStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      
        textView.append(style) ;
        Log.d(logTag, logStr) ;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView)findViewById(R.id.logView) ;
        _dummyLog("onCreate call") ;
        self = this;
        smsButton = (Button)findViewById(R.id.viewSmsButton) ;
        smsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(self, HelloSms.class);
				// startActivity(intent);
				startActivityForResult(intent, 0) ;
			}
		}) ;
        
        Button viewPic = (Button)findViewById(R.id.viewPicButton) ;
        viewPic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(self, HelloPictureTest.class);
				startActivity(intent);
			}
		}) ;
        
        uiTestButton = (Button)findViewById(R.id.uiTestButton) ;
        uiTestButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HelloBroadcastRecv.setCurActivity(null) ;
				Intent intent = new Intent(self, HelloUiTest.class);
				startActivity(intent);
				// startActivityForResult(intent, 0) ;
			}
		}) ;
        
    }
    	
    @Override
    public void onStart() {
    	super.onStart() ;
    	_dummyLog("onStart call") ;
    }
    
    @Override
    public void onResume() {
    	super.onResume() ;
    	HelloBroadcastRecv.setCurActivity(self) ;
    	_dummyLog("onResume call") ;
    }
    
    @Override
    public void onPause() {
    	super.onPause() ;
    	_dummyLog("onPause call") ;
    }
    
    @Override
    public void onStop() {
    	super.onStop() ;
    	_dummyLog("onStop call") ;
    }
    
    @Override
    public void onRestart() {
    	super.onRestart() ;
    	_dummyLog("onRestart call") ;
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy() ;
    	// _dummyLog("onDestroy call") ;
    }
    
    @Override
    protected void onSaveInstanceState (Bundle outState) {
    	super.onSaveInstanceState(outState) ;
    	_dummyLog("onSaveInstanceState call") ;
    }
    
    public final void onSmsReceive(Intent data) {
    	_dummyLog("onSmsReceive is call and active the HelloSms activity") ;
    	
		Intent intent = new Intent(self, HelloSms.class);
			
		Bundle bundle = new Bundle();
		bundle.putBundle("smsData", data.getExtras());
		intent.putExtras(bundle) ;
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
		startActivityForResult(intent, 1) ;
		// startActivity(intent);
    }
    
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (data != null && data.getExtras() != null) {
        	String result = data.getExtras().getString("result");
        	_dummyLog(result + " requestCode:" + requestCode + " resultCode:" + resultCode) ;
    	}
    }

}